'use strict';

const semver = require('semver')
const colors = require('colors')
const log = require('@fs-cli-dev/log')
const utils = require('@fs-cli-dev/utils')

const LOWEST_NODE_VERSION = "12.0.0"

class Command {
    constructor(argv) {
        log.verbose('Command constructor:', argv);
        this._argv = argv
        if (!argv) {
            throw new Error('Command 初始化 参数不能为空')
        }
        if (utils.checkType(argv) !== 'array') {
            throw new Error('参数必须是数组')
        }
        if (argv.length < 1) {
            throw new Error('参数列表为空')
        }
        let runner = new Promise((resolve, reject) => {
            let chain = Promise.resolve()
            chain = chain.then(() => this.checkNodeVersion())
            chain = chain.then(() => this.initArgs())
            chain = chain.then(() => this.init())
            chain = chain.then(() => this.exec())
            chain.catch(err => {
                log.error(colors.red(err.message))
            })
        })
    }

    initArgs() {
        this._cmd = this._argv.slice(-1)[0] || {}
        this._argv = this._argv.slice(0, -1)
    }

    // 检查node版本号
    checkNodeVersion() {
        // 获取当前Node版本号
        const currentVersion = process.version

        // 比对最低版本号
        // LOWEST_NODE_VERSION
        if (!semver.gte(currentVersion, LOWEST_NODE_VERSION)) {
            throw new Error(colors.red('fs-cli: node最低版本是', LOWEST_NODE_VERSION, '请升级node'))
        }

    }
    init() {
        throw new Error('init 必须实现')

    }

    exec() {
        throw new Error('exec 必须实现')
    }
}


module.exports = Command;